#!/bin/bash

# API测试脚本 - 使用curl测试所有API接口
# 1. 先使用admin/admin登录获取token
# 2. 使用token测试其他接口

# 配置基础URL
BASE_URL="http://localhost:8000/api/v1"

# 存储token的变量
AUTH_TOKEN=""

# 颜色输出定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo -e "${YELLOW}开始API测试...${NC}"

# 登录函数
login() {
    echo -e "\n${YELLOW}正在登录...${NC}"
    response=$(curl -s -w "%{http_code}" -X POST "${BASE_URL}/auth/login" \
        -H "Content-Type: application/json" \
        -d '{"username":"admin","password":"admin"}')
    
    # 提取HTTP状态码（最后3位）
    http_code="${response: -3}"
    
    # 提取响应体（除最后3位）
    body="${response%???}"
    
    if [ "$http_code" = "200" ]; then
        AUTH_TOKEN=$(echo "$body" | grep -o '"access_token":"[^"]*"' | cut -d'"' -f4)
        if [ -n "$AUTH_TOKEN" ]; then
            echo -e "${GREEN}登录成功，获取到token${NC}"
            echo "Token: $AUTH_TOKEN"
            return 0
        else
            echo -e "${RED}登录失败，无法提取token${NC}"
            echo "响应内容: $body"
            return 1
        fi
    else
        echo -e "${RED}登录失败，HTTP状态码: $http_code${NC}"
        echo "响应内容: $body"
        return 1
    fi
}

# 测试需要认证的接口
test_authenticated_apis() {
    if [ -z "$AUTH_TOKEN" ]; then
        echo -e "${RED}没有有效的token，无法测试认证接口${NC}"
        return 1
    fi
    
    local headers=(
        "-H" "Authorization: Bearer $AUTH_TOKEN"
        "-H" "Content-Type: application/json"
    )
    
    # 测试获取用户信息
    echo -e "\n${YELLOW}--- 测试获取用户信息 ---${NC}"
    response=$(curl -s -w "%{http_code}" -X GET "${BASE_URL}/auth/userinfo" "${headers[@]}")
    http_code="${response: -3}"
    body="${response%???}"
    if [ "$http_code" = "200" ]; then
        echo -e "${GREEN}获取用户信息成功${NC}"
        echo "响应: $body"
    else
        echo -e "${RED}获取用户信息失败，HTTP状态码: $http_code${NC}"
        echo "响应: $body"
    fi
    
    # 测试用户管理接口
    echo -e "\n${YELLOW}--- 测试获取用户列表 ---${NC}"
    response=$(curl -s -w "%{http_code}" -X GET "${BASE_URL}/users/list" "${headers[@]}")
    http_code="${response: -3}"
    body="${response%???}"
    if [ "$http_code" = "200" ]; then
        echo -e "${GREEN}获取用户列表成功${NC}"
        echo "响应: $body"
    else
        echo -e "${RED}获取用户列表失败，HTTP状态码: $http_code${NC}"
        echo "响应: $body"
    fi
    
    # 测试学生管理接口
    echo -e "\n${YELLOW}--- 测试获取学生列表 ---${NC}"
    response=$(curl -s -w "%{http_code}" -X GET "${BASE_URL}/students/list" "${headers[@]}")
    http_code="${response: -3}"
    body="${response%???}"
    if [ "$http_code" = "200" ]; then
        echo -e "${GREEN}获取学生列表成功${NC}"
        echo "响应: $body"
    else
        echo -e "${RED}获取学生列表失败，HTTP状态码: $http_code${NC}"
        echo "响应: $body"
    fi
    
    # 测试教师管理接口
    echo -e "\n${YELLOW}--- 测试获取教师列表 ---${NC}"
    response=$(curl -s -w "%{http_code}" -X GET "${BASE_URL}/teachers/list" "${headers[@]}")
    http_code="${response: -3}"
    body="${response%???}"
    if [ "$http_code" = "200" ]; then
        echo -e "${GREEN}获取教师列表成功${NC}"
        echo "响应: $body"
    else
        echo -e "${RED}获取教师列表失败，HTTP状态码: $http_code${NC}"
        echo "响应: $body"
    fi
    
    # 测试教室管理接口
    echo -e "\n${YELLOW}--- 测试获取教室列表 ---${NC}"
    response=$(curl -s -w "%{http_code}" -X GET "${BASE_URL}/classrooms/list" "${headers[@]}")
    http_code="${response: -3}"
    body="${response%???}"
    if [ "$http_code" = "200" ]; then
        echo -e "${GREEN}获取教室列表成功${NC}"
        echo "响应: $body"
    else
        echo -e "${RED}获取教室列表失败，HTTP状态码: $http_code${NC}"
        echo "响应: $body"
    fi
    
    # 测试课程管理接口
    echo -e "\n${YELLOW}--- 测试获取课程列表 ---${NC}"
    response=$(curl -s -w "%{http_code}" -X GET "${BASE_URL}/courses/list" "${headers[@]}")
    http_code="${response: -3}"
    body="${response%???}"
    if [ "$http_code" = "200" ]; then
        echo -e "${GREEN}获取课程列表成功${NC}"
        echo "响应: $body"
    else
        echo -e "${RED}获取课程列表失败，HTTP状态码: $http_code${NC}"
        echo "响应: $body"
    fi
    
    # 测试学年管理接口
    echo -e "\n${YELLOW}--- 测试获取学年列表 ---${NC}"
    response=$(curl -s -w "%{http_code}" -X GET "${BASE_URL}/academic-years/list" "${headers[@]}")
    http_code="${response: -3}"
    body="${response%???}"
    if [ "$http_code" = "200" ]; then
        echo -e "${GREEN}获取学年列表成功${NC}"
        echo "响应: $body"
    else
        echo -e "${RED}获取学年列表失败，HTTP状态码: $http_code${NC}"
        echo "响应: $body"
    fi
    
    # 测试班级管理接口
    echo -e "\n${YELLOW}--- 测试获取班级列表 ---${NC}"
    response=$(curl -s -w "%{http_code}" -X GET "${BASE_URL}/classes/list" "${headers[@]}")
    http_code="${response: -3}"
    body="${response%???}"
    if [ "$http_code" = "200" ]; then
        echo -e "${GREEN}获取班级列表成功${NC}"
        echo "响应: $body"
    else
        echo -e "${RED}获取班级列表失败，HTTP状态码: $http_code${NC}"
        echo "响应: $body"
    fi
    
    # 测试班级学生管理接口
    echo -e "\n${YELLOW}--- 测试获取班级学生列表 ---${NC}"
    response=$(curl -s -w "%{http_code}" -X GET "${BASE_URL}/class-students/list" "${headers[@]}")
    http_code="${response: -3}"
    body="${response%???}"
    if [ "$http_code" = "200" ]; then
        echo -e "${GREEN}获取班级学生列表成功${NC}"
        echo "响应: $body"
    else
        echo -e "${RED}获取班级学生列表失败，HTTP状态码: $http_code${NC}"
        echo "响应: $body"
    fi
    
    # 测试角色管理接口
    echo -e "\n${YELLOW}--- 测试获取角色列表 ---${NC}"
    response=$(curl -s -w "%{http_code}" -X GET "${BASE_URL}/roles/list" "${headers[@]}")
    http_code="${response: -3}"
    body="${response%???}"
    if [ "$http_code" = "200" ]; then
        echo -e "${GREEN}获取角色列表成功${NC}"
        echo "响应: $body"
    else
        echo -e "${RED}获取角色列表失败，HTTP状态码: $http_code${NC}"
        echo "响应: $body"
    fi
    
    # 测试用户角色管理接口
    echo -e "\n${YELLOW}--- 测试获取用户角色列表 ---${NC}"
    response=$(curl -s -w "%{http_code}" -X GET "${BASE_URL}/user-roles/list" "${headers[@]}")
    http_code="${response: -3}"
    body="${response%???}"
    if [ "$http_code" = "200" ]; then
        echo -e "${GREEN}获取用户角色列表成功${NC}"
        echo "响应: $body"
    else
        echo -e "${RED}获取用户角色列表失败，HTTP状态码: $http_code${NC}"
        echo "响应: $body"
    fi
    
    # 测试权限管理接口
    echo -e "\n${YELLOW}--- 测试获取权限列表 ---${NC}"
    response=$(curl -s -w "%{http_code}" -X GET "${BASE_URL}/permissions/list" "${headers[@]}")
    http_code="${response: -3}"
    body="${response%???}"
    if [ "$http_code" = "200" ]; then
        echo -e "${GREEN}获取权限列表成功${NC}"
        echo "响应: $body"
    else
        echo -e "${RED}获取权限列表失败，HTTP状态码: $http_code${NC}"
        echo "响应: $body"
    fi
    
    # 测试角色权限管理接口
    echo -e "\n${YELLOW}--- 测试获取角色权限列表 ---${NC}"
    response=$(curl -s -w "%{http_code}" -X GET "${BASE_URL}/role-permissions/list" "${headers[@]}")
    http_code="${response: -3}"
    body="${response%???}"
    if [ "$http_code" = "200" ]; then
        echo -e "${GREEN}获取角色权限列表成功${NC}"
        echo "响应: $body"
    else
        echo -e "${RED}获取角色权限列表失败，HTTP状态码: $http_code${NC}"
        echo "响应: $body"
    fi
}

# 主函数
main() {
    # 登录获取token
    if login; then
        # 使用token测试其他接口
        test_authenticated_apis
    else
        echo -e "${RED}登录失败，无法继续测试${NC}"
        exit 1
    fi
    
    echo -e "\n${YELLOW}API测试结束${NC}"
}

# 执行主函数
main